//
//  SNHomeHeadView.m
//  project
//
//  Created by 圆圆陈 on 2018/1/26.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "SNHomeHeadView.h"
#import "ZZCACircleProgress.h"
#import "SNStepChartVc.h"

@interface SNHomeHeadView ()
@property (nonatomic, strong) ZZCACircleProgress            *stepCircle; // 步数圆圈
@property (nonatomic, strong) SNStepChartView               *stepChartView; // 详情
@property (nonatomic, strong) SNCustomLabel                 *distanceLabel;
@property (nonatomic, strong) SNCustomLabel                 *StepLabel;
@property (nonatomic, strong) SNCustomLabel                 *calorLabel;
@property (nonatomic, strong) SNHomePageShowStepDetailLabel *distanceUnitLabel;
@property (nonatomic, strong) SNHomePageShowStepDetailLabel *stepUnitLabel;
@property (nonatomic, strong) SNHomePageShowStepDetailLabel *calorUnitLabel;
@property (nonatomic, strong) UIView                        *otherView;
@end

@implementation SNHomeHeadView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        CGFloat xCrack    = ([UIScreen mainScreen].bounds.size.width)/3.0;
        CGFloat yCrack    = 10;
        CGFloat itemWidth = xCrack + 50;
        _stepCircle                  = [[ZZCACircleProgress alloc] initWithFrame:CGRectMake(xCrack - 30, yCrack, itemWidth, itemWidth) pathBackColor:nil pathFillColor:SNCOLORBLACK startAngle:-90 strokeWidth:4.5];
        _stepCircle.unitLabel.font = SNSFONT(18.0);
        _stepCircle.showProgressText = YES;
        _stepCircle.duration         = 0.5; //动画时长
        _stepCircle.pointImage.image = [UIImage imageNamed:@"circle_point2"];
        
        //        if ([[NSDate date].dateToDayString isEqualToString:[SN_PublicClass.selectDate dateToDayString]]) {
        //            _stepCircle.increaseFromLast = YES; // 如果是今天就从当前进度开始
        //        } else {
        //            _stepCircle.increaseFromLast = NO;
        //        }
        
        _stepCircle.prepareToShow = YES;//设置好属性，准备好显示了，显示之前必须调用一次
        [self addSubview:_stepCircle];
        
        UIButton *stepDetailButton = [UIButton buttonWithType:UIButtonTypeCustom];
        stepDetailButton.frame = CGRectMake(HHHWIDTH - 55, FitSimpleSize(100, 125, 145, 160, 170), 44, 44);
        [stepDetailButton setImageNormal:@"右"];
        [stepDetailButton addTarget:self
                             action:@selector(tapChangeView)
                   forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:stepDetailButton];
        
        _StepLabel        = [[SNCustomLabel alloc] initWithFrame:SNFRAME(0, 0, 100, 20)];
        _StepLabel.center = CGPointMake(HHHWIDTH /2, _stepCircle.origin.y + _stepCircle.width + 22);
        [self addSubview:_StepLabel];
        
        _distanceLabel        = [[SNCustomLabel alloc] initWithFrame:SNFRAME(0, 0, 100, 20)];
        _distanceLabel.center = CGPointMake(HHHWIDTH /4 -30, _stepCircle.origin.y + _stepCircle.width + 22);
        _distanceLabel.text   = @"0.00";
        [self addSubview:_distanceLabel];
        
        _calorLabel        = [[SNCustomLabel alloc] initWithFrame:SNFRAME(0, 0, 100, 20)];
        _calorLabel.center = CGPointMake(HHHWIDTH /4*3 +30, _stepCircle.origin.y + _stepCircle.width + 22);
        _calorLabel.text   = @"0";
        [self addSubview:_calorLabel];
        
        _stepUnitLabel        = [[SNHomePageShowStepDetailLabel alloc] initWithFrame:SNFRAME(0, 0, 100, 20)];
        _stepUnitLabel.center = CGPointMake(HHHWIDTH /2, _stepCircle.origin.y + _stepCircle.width + 23 +25);
        [self addSubview:_stepUnitLabel];
        
        _distanceUnitLabel        = [[SNHomePageShowStepDetailLabel alloc] initWithFrame:SNFRAME(0, 0, 100, 20)];
        _distanceUnitLabel.center = CGPointMake(HHHWIDTH /4 - 25, _stepCircle.origin.y + _stepCircle.width + 23 +25);
        [self addSubview:_distanceUnitLabel];
        
        _calorUnitLabel        = [[SNHomePageShowStepDetailLabel alloc] initWithFrame:SNFRAME(0, 0, 100, 20)];
        _calorUnitLabel.center = CGPointMake(HHHWIDTH /4*3 + 45, _stepCircle.origin.y + _stepCircle.width + 23 +25);
        [self addSubview:_calorUnitLabel];
        
        //单位
        _stepUnitLabel.label.text     = SN_Text(@"目标步数");
        _StepLabel.text               = [NSString stringWithFormat:@"%ld", SN_USER.target_step];
        _distanceUnitLabel.label.text = SN_USER.isMetricSystem ? [NSString stringWithFormat:@"%@ %@", SN_Text(@"里程"), @"mile"] : [NSString stringWithFormat:@"%@ %@", SN_Text(@"里程"), @"km"];
        
        _calorUnitLabel.label.text = SN_Text(@"大卡");
        
        _stepChartView        = [[SNStepChartView alloc] initWithFrame:SNFRAME(0, 0, HHHWIDTH, self.height)];
        _stepChartView.hidden = YES;
        [self addSubview:_stepChartView];
        
        UIButton *stepLeftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        stepLeftButton.frame = CGRectMake(10, 0, 44, 44);
        [stepLeftButton setImageNormal:@"左"];
        [stepLeftButton addTarget:self
                           action:@selector(backView)
                 forControlEvents:UIControlEventTouchUpInside];
        stepLeftButton.hidden = YES;
        [self addSubview:stepLeftButton];
        
        UITapGestureRecognizer *steptapGesture1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapChangeView)];
        UITapGestureRecognizer *steptapGesture2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapChangeView)];
        UITapGestureRecognizer *steptapGesture3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapChangeView)];
        
        _otherView                 = [[UIView alloc] initWithFrame:SNFRAME(0, 0, HHHWIDTH, self.height)];
        _otherView.hidden          = YES;
        _otherView.backgroundColor = [UIColor redColor];
        [self addSubview:_otherView];
        
        [_stepCircle addGestureRecognizer:steptapGesture1];
        [_stepChartView addGestureRecognizer:steptapGesture2];
        [_otherView addGestureRecognizer:steptapGesture3];
        _selectIndex = 1;
    }
    return self;
}

- (void)setModel:(SportModel *)model {
    _model = model;
    //圆圈里面数值
    int stepTarget = 0;
    
    _stepCircle.progress = model.step*1.0/model.stepTarget;
    //    _stepCircle.ValueLabel.text = [NSString stringWithFormat:@"%ld", model.step];
    NSInteger lastStep = [[LASTSTEP getObjectValue] intValue];
    //    NSLog(@"更新前步数 >>>%d 当前步数>>>%d",lastStep,model.step);
    
    if ([model.dateString isEqualToString:[[NSDate date]dateToDayString]]) {
        if (lastStep < model.step) {
            [_stepCircle.ValueLabel countingFrom:(CGFloat)lastStep to:(CGFloat)model.step duration:1.0];
        } else {
            lastStep                    = model.step;
            _stepCircle.ValueLabel.text = [NSString stringWithFormat:@"%ld", model.step];
        }
        _stepCircle.increaseFromLast = YES;
        [LASTSTEP setObjectValue:StrByInt(model.step)];
    } else {
        _stepCircle.increaseFromLast = NO;
        _stepCircle.ValueLabel.text  = [NSString stringWithFormat:@"%ld", model.step];
    }
    
    //数值
    _distanceUnitLabel.label.text = SN_USER.isMetricSystem ? [NSString stringWithFormat:@"%@ %@", SN_Text(@"里程"), @"mile"] : [NSString stringWithFormat:@"%@ %@", SN_Text(@"里程"), @"km"];
    int   distance  = model.distance /10 *10;
    float distanceS = distance *1.0 /1000;
    
    int   distanceReal  = model.realDistance /10 *10;
    float distanceSReal = distanceReal *1.0 /1000;
    
    if ([model.dateString isEqualToString:[NSDate date].dateToDayString]) {
        _distanceLabel.text = [NSString stringWithFormat:@"%.2f", distanceSReal];
    } else {
        _distanceLabel.text = [NSString stringWithFormat:@"%.2f", distanceSReal];
    }
    _calorLabel.text = [NSString stringWithFormat:@"%ld", model.calory];
    _StepLabel.text  = [NSString stringWithFormat:@"%ld", model.stepTarget];
    [_stepChartView UpdateViewWithType:SelectDateDay withArray:_model.stepArray];
}

- (void)setHidden:(BOOL)hidden {
    _hidden               = hidden;
    _stepChartView.hidden = hidden;
//    _otherView.hidden     = hidden;

    if (_selectIndex == 2) {
        _otherView.hidden = YES;
    }
    if (_selectIndex == 3) {
        _stepChartView.hidden = YES;
    }
}

- (void)tapChangeView {
    if (_selectIndex < 2) {
        _selectIndex++;
    } else {
        _selectIndex = 1;
    }
    NSLog(@"主页详情切换>>>%d", _selectIndex);
    _stepChartView.hidden = YES;
    _otherView.hidden     = YES;
    if (_selectIndex == 2) {
        _stepChartView.hidden = NO;
    }
    if (_selectIndex == 3) {
        _otherView.hidden = NO;
    }
    
    BOOL hid = NO;
    if (_selectIndex == 1) {
        hid = YES;
    } else {
        hid = NO;
    }
    if (_hiddenBlock) {
        _hiddenBlock(hid);
    }
}

- (void)backView {
}

@end

